package Main;

import Other.Field;
import Robots.Robot1;
import Units.UnitName;
import jgame.JGColor;
import jgame.JGObject;
import jgame.JGPoint;
import jgame.platform.JGEngine;

public class Game extends JGEngine {
	public static Board board = null;
	
	public static void main(String[] args){
		new Game(new JGPoint(960, 640));
	}
	
	/** Application constructor. */
	public Game(JGPoint size) { initEngine(size.x,size.y); }

	/** Applet constructor. */
	public Game() { initEngineApplet(); }

	@Override
	public void initCanvas() {
		setCanvasSettings(80,80,12,8,JGColor.black,new JGColor(255,246,199),null);		
	}
	
	public int x = 0, y = 0;
	public boolean crtaj = true;
	public Robot1 robot1;

	@Override
	public void initGame() {
		setFrameRate(35, 2);
		defineMedia("dvr.tbl");
		setBGImage("background");
		board = new Board();
	}
	
	public void doFrame() {
		moveObjects();
		
		if (getMouseButton(1)){
			clearMouseButton(1);
			x = getMouseX();
			y = getMouseY();
			crtaj = true;
			
			if (!board.selected){
				Field f = board.getField(getMouseX(), getMouseY());
				int cid = board.getCID(f.i, f.j);
				
				if (cid != 0){
					System.out.println("ASD " + cid);
					// unit has been selected
					board.selected = true;
					
					// check if robot1 or dragon1
					boolean found = false;
					for (int i = 0; i < 5 && !found; i++){
						if (cid == board.robots1[i].getCID()){
							found = true;
							board.setAllowed(board.robots1[i]);
						}
						else if (cid == board.dragons1[i].getCID()){
							found = true;
							board.setAllowed(board.dragons1[i]);
						}
					}
					
					// check if robot2 or dragon2
					for (int i = 0; i < 2 && !found; i++){
						if (cid == board.robots2[i].getCID()){
							found = true;
							board.setAllowed(board.robots2[i]);
						}
						else if (cid == board.dragons2[i].getCID()){
							found = true;
							board.setAllowed(board.dragons2[i]);
						}
					}
					
					// check if robot3 or dragon3
					for (int i = 0; i < 2 && !found; i++){
						if (cid == board.robots3[i].getCID()){
							found = true;
							board.setAllowed(board.robots3[i]);
						}
						else if (cid == board.dragons3[i].getCID()){
							found = true;
							board.setAllowed(board.dragons3[i]);
						}
					}
					
					// check if robot4 or dragon4
					for (int i = 0; i < 2 && !found; i++){
						if (cid == board.robots4[i].getCID()){
							found = true;
							board.setAllowed(board.robots4[i]);
						}
						else if (cid == board.dragons4[i].getCID()){
							found = true;
							board.setAllowed(board.dragons4[i]);
						}
					}
					
					if (!found){
						// check if robotKing or dragonKing
						if (cid == board.robotKing.getCID()){
							found = true;
							board.setAllowed(board.robotKing);
						}
						else if (cid == board.dragonKing.getCID()){
							found = true;
							board.setAllowed(board.dragonKing);
						}
					}
				}
			}
			else {	// unit is selected
				Field f = board.getField(getMouseX(), getMouseY());
				int cid = board.getCID(f.i, f.j);
				
				if (cid == board.getSelectedUnitID()){
					board.selected = false;
					board.resetAllowed();
				}
			}			
		}
		else {
			crtaj = false;
//			board.robots1[0].yspeed = 0.4;
//			board.robots1[0].setGraphic(Robot1.stringMoveDown);
		}
	}
	
	public void paintFrame() {
		// draw some instructions
		setColor(JGColor.white);
		drawString("MouseX = " + x + "   MouseY = " + y,
				pfWidth()/2, 110, 0);
		
		if (crtaj){
			//drawImage(x - 82/2, y - 74/2, "robot1");
			Field f = board.getField(getMouseX(), getMouseY());
			drawString("Field = (" + f.i + ", " + f.j +")", 300, 300, 0);
		}
		
		for (int i = 0; i < board.getHeight(); i++){
			for (int j = 0; j < board.getWidht(); j++){
				if (!board.inBoard(i, j) || !board.isAllowed(i, j)) continue;
				
				JGPoint p = board.getRealCoordinates(i, j);
				
				drawImage(p.x - 55, p.y - 77/2, "field_mouseover", true);
			}
		}
	}
}
